//
//  ConvenientPayOrderEngine.m
//  HuLaQuan
//
//  Created by hok on 2/5/16.
//  Copyright © 2016 yuwubao. All rights reserved.
//

#import "ConvenientPayOrderEngine.h"

#import "CurrentUser.h"

@implementation ConvenientPayOrderEngine

- (void)getOrderInfoWithID:(NSString *)resID andType:(NSString *)typeCode andCompany:(NSString *)companyID
{
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSString *src;
    if([typeCode isEqualToString:@"gzWater"])
    {
        src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"getOrder\" sysName=\"attach\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@\" userId=\"%@\" channelId=\"88888888\" mchntId=\"%@\"><appCode>%@</appCode><resId>%@</resId><eparchyCode>0851</eparchyCode><attach>attach</attach></msg>",[self getSerialNumber],[CurrentUser sharedInstance].token,[CurrentUser sharedInstance].ID ,companyID,typeCode,resID];
    }else{
       src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"getOrder\" sysName=\"attach\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@\" userId=\"%@\" channelId=\"88888888\" mchntId=\"%@\"><appCode>%@</appCode><resId>%@</resId><attach>attach</attach></msg>",[self getSerialNumber],[CurrentUser sharedInstance].token,[CurrentUser sharedInstance].ID ,companyID,typeCode,resID];
    }
    
    

    NSLog(@"%@",src);
    
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",[self firstString],[self secondStringWithKey:randomKey],[self thirdStringWithBody:src andKey:randomKey]];
    
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_CONPAY parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            
            NSString *resString = [_DES decrypt:[item lastObject] withKey:randomKey];
            
            NSDictionary *resultDic = [self parseXMLFromYinLianConnection:resString];
            
            /**
             *  return OrderInfo;
             */
            
            result = @{@"isSuccess":@YES,@"result":resultDic};
        }else{
            result = @{@"isSuccess":@NO,@"message":item};
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"GetCPOrderInfoFinish" object:result];
    }];

}

- (void)getPhoneOrderInfoWithID:(NSString *)resID andType:(NSString *)typeCode andCompany:(NSString *)companyID andValue:(NSString *)value
{
    
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSString *src;
    
    src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"getOrder\" sysName=\"attach\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@\" userId=\"%@\" channelId=\"88888888\" mchntId=\"%@\"><appCode>%@</appCode><resId>%@</resId><attach>{\"txnAmt\":%@}</attach></msg>",[self getSerialNumber],[CurrentUser sharedInstance].token,[CurrentUser sharedInstance].ID ,companyID,typeCode,resID, ConvertToServerMoney(value)];
    
    
    
    NSLog(@"%@",src);
    
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",[self firstString],[self secondStringWithKey:randomKey],[self thirdStringWithBody:src andKey:randomKey]];
    
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_CONPAY parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            
            NSString *resString = [_DES decrypt:[item lastObject] withKey:randomKey];
            
            NSDictionary *resultDic = [self parseXMLFromYinLianConnection:resString];
            
            /**
             *  return OrderInfo;
             */
            
            result = @{@"isSuccess":@YES,@"result":resultDic};
        }else{
            result = @{@"isSuccess":@NO,@"message":item};
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"GetCPOrderInfoFinish" object:result];
    }];

}
/**
 * 电费返回
 *  appCode = gzEle;
 attach = "{\"accrualDate\":\"201512\",\"contRespDesc\":\"SUCCESS\",\"partPayTag\":\"1\",\"resName\":\"\U5218\U671d\U5149\",\"contRespCode\":\"00\",\"billFlag\":\"0106\",\"resId\":\"0601140006278540\",\"bankId\":\"\",\"amount\":\"5\",\"demurrage\":\"0\",\"payAmt\":\"5\",\"feeType\":\"010\",\"accTradeId\":\"0\",\"unitCode\":\"06010608\"}";
 notifySysName = lifeFee;
 notifySysUrl = "http://219.140.165.62:8080/";
 orderId = 1602050000028814;
 orderSummary = "\U7535\U8d39\U8d26\U5355\U67e5\U8be2";
 payAmt = 5;
 payTag = 1;
 resChannelId = 00000000;
 resId = 0601140006278540;
 resName = "\U5218\U671d\U5149";
 resType = LifeAssistant;
 txnAmt = 5;
 */


/**
 * 燃气费返回
 *  appCode = gzGas;
 attach = "{\"billAppCode\":\"01\",\"arrearage\":\"0\",\"curUsage\":\"296\",\"contRespDesc\":\"\U4ea4\U6613\U6210\U529f\",\"unitPrice\":\"135\",\"contRespCode\":\"00\",\"resName\":\"\U6d82\U4fdd\U6797\",\"resId\":\"21345678907\",\"redoFee\":\"200\",\"demurrage\":\"0\",\"lastUsage\":\"215\",\"curFee\":\"11160\",\"payStatus\":\"0\",\"payAmt\":\"1\",\"usages\":\"81\",\"comments\":\"\U7164\U6c14\U67e5\U8be2\"}";
 notifySysName = lifeFee;
 notifySysUrl = "http://219.140.165.62:8080/";
 orderId = 1602050000028818;
 orderSummary = "\U7164\U6c14\U8d26\U5355\U67e5\U8be2";
 payAmt = 1;
 payTag = 1;
 resChannelId = 00000000;
 resId = 21345678907;
 resName = "\U6d82\U4fdd\U6797";
 resType = LifeAssistant;
 txnAmt = 1;
 */


/**
 * 交通罚单
 *  appCode = gzTraFine;
 attach = "{\"demurrageDays\":\"345\",\"billAppCode\":\"01\",\"roadNo\":\"\",\"contRespDesc\":\"\U4ea4\U6613\U6210\U529f\",\"punishDate\":\"20120904\",\"contRespCode\":\"00\",\"resName\":\"\U5ed6\U521a\",\"demurrage\":\"10000\",\"certifId\":\"522523197312070110\",\"phoneNo\":\"\",\"deductScore\":\"\",\"punishFee\":\"10000\",\"punishDept\":\"520100\",\"payAmt\":\"2\",\"fileNo\":\"520100315720\",\"plateNumber\":\"\U8d35AS6211\",\"comments\":\"\U4ea4\U7f5a\U67e5\U8be2\"}";
 notifySysName = lifeFee;
 notifySysUrl = "http://219.140.165.62:8080/";
 orderId = 1602050000028822;
 orderSummary = "\U4ea4\U7f5a\U8d26\U5355\U67e5\U8be2";
 payAmt = 2;
 payTag = 1;
 resChannelId = 00000000;
 resId = 520100315720;
 resName = "\U5ed6\U521a";
 resType = LifeAssistant;
 txnAmt = 2;
 */
@end
